package servlet.admin;


import com.alibaba.fastjson.JSONObject;
import entity.Borrow_card;
import service.admin.CardEditService;
import service.admin.impl.CardEditServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * CardEditServlet
 *
 * @author lenovo
 * @since 2023/9/5
 */
@WebServlet("/admin/cardEdit")
public class CardEditServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    //引入服务层
    CardEditService service = new CardEditServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        // 接受数据
        String id = req.getParameter("id");
        String password = req.getParameter("password");
        String reader = req.getParameter("reader");
        String rule_id = req.getParameter("rule_id");
        String status = req.getParameter("status");

        //初始数据
        int result = 0;

        String code = "1";
        String msg = "error";
        JSONObject json = new JSONObject();
        PrintWriter out = resp.getWriter();
        // 判断数据
        if(id == null || password == null || reader == null || rule_id == null || status == null ||
                id.equals("") || password.equals("") || reader.equals("") || rule_id.equals("") || status.equals("")) {
            code = "1";
            msg = "参数不能为空";
        }
        //创建对象
        Borrow_card card = new Borrow_card(Integer.valueOf(id),password,reader,Integer.valueOf(rule_id),Integer.valueOf(status));
        result = service.update(card);

        if (result == 1){
            code = "0";
            msg = "修改成功";
        }
        json.put("code", code);
        json.put("msg", msg);
        out.print(json.toJSONString());
        out.flush();
        out.close();
    }
}
